<?php

namespace app\admin\controller\resource;

use app\common\controller\Backend;
use app\common\model\Resource as ResourceModel;
use app\common\model\Document;
use think\Db;
/**
 * 谋士管理
 *
 * @icon fa fa-user-secret
 */
class Index extends Backend
{
    protected $model = null;
    protected $searchFields = ['name', 'type'];
    protected $categoryModel = null;
    protected $relationSearch = true;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new ResourceModel;
    }
    /**
     * 查看
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->with(['type'])
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);
            $result = array(
                "total" => $list->total(),
                "rows" => $list->items()
            );
            return json($result);
        }
        return $this->view->fetch();
    }
    public function addb(){
        if($this->request->isAjax()){
           $data =  Db::name('UserResource')->where('status','normal')->select();
           return json(['code'=>1,'data'=>$data]);
        }
    }
    public function add(){
        if ($this->request->isPost()) {
            $params = $this->request->post('row/a');
            Db::startTrans();
            try {

               $row =  $this->model->create($params,  true);
                $document['doc_id'] = $row->id;
                $document['title'] = $row->title;
                $document['content'] = $row->content;
                $document['createtime'] = $row->createtime;
                $document['updatetime'] = $row->updatetime;
                $document['type'] = 'wr';
                $document['is_audit'] = 1;
                Document::create($document,true);
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            $this->success();
        }
        $item = Db::name('ResourceItem')->where('type_id',input('type_id'))->order('id asc')->select();
   
        foreach ($item as $key => $value) {
            $arr = explode(',', $value['content']);
            foreach ($arr as $k => $v) {
                $form[$key][$v] =$v;# code...
            }
        }
        $this->assign('type_id', input('type_id'));
        $this->view->assign('item', $item);
        $this->view->assign('form', $form);
        return $this->view->fetch();
    }

    public function edit($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }

        if ($this->request->isPost()) {
            $params = $this->request->post('row/a');
            Db::startTrans();
            try {

                $row->allowField(true)->save($params);
                $document['doc_id'] = $row->id;
                $document['title'] = $row->title;
                $document['content'] = $row->content;
                $document['createtime'] = $row->createtime;
                $document['updatetime'] = $row->updatetime;
                Db::name('Document')->where('doc_id', $ids)->where('type','wr')->update($document);
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            $this->success();
        }
        $item = Db::name('ResourceItem')->where('type_id',$row['type_id'])->order('id asc')->select();
        foreach ($item as $key => $value) {
            $arr = explode(',', $value['content']);
            foreach ($arr as $k => $v) {
                $form[$key][$v] =$v;# code...
            }
        }
        $this->view->assign('item', $item);
        $this->view->assign('form', $form);
        $this->view->assign('row', $row);
        return $this->view->fetch();
    }
}